package com.xinke.edu.mypetadoption;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.xinke.edu.mypetadoption.databinding.ActivityMerchantBinding;

public class MerchantActivity extends AppCompatActivity {
    ActivityMerchantBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMerchantBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());

        String[] labels = {"选项A", "选项B", "选项C"};
        int[] values = {1, 2, 3};

        // 适配器绑定文字
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_spinner_item,
                labels
        );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // 直接用 binding 访问 Spinner
        binding.mySpinner.setAdapter(adapter);

        // 初始化标志位，避免第一次触发
        boolean[] isInitialized = {false};

        binding.mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                if (!isInitialized[0]) {
                    isInitialized[0] = true;
                    return; // 第一次初始化不处理
                }
                int selectedValue = values[i];
                String selectedText = labels[i];

                Toast.makeText(getApplicationContext(),
                        "选中：" + selectedText + "（值：" + selectedValue + "）",
                        Toast.LENGTH_SHORT).show();

                // 根据 selectedValue 做逻辑判断
                if (selectedValue == 1) {
                    // do something
                    return;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }
}